from collections import OrderedDict
import datetime
import output

def merge_routers(data, perc):
    print len(data.keys())
    
#    for router in data.items():
#        print router
        
    router_set_desc = OrderedDict(sorted(data.items(), reverse = True, key=lambda x: len(x[1]))).keys()
    router_set_asc = OrderedDict(sorted(data.items(), reverse = False, key=lambda x: len(x[1]))).keys()
    for router_a in router_set_asc:
        for router_b in router_set_desc:
            if(data.has_key(router_b) and data.has_key(router_a) and router_b != router_a):
                data_router_a = set([item[0] for item in data[router_a]])
                data_router_b = set([item[0] for item in data[router_b]])
#                print data_router_a
#                print data_router_b
                
                counter = 0
                for item in data_router_b:
                    if item in data_router_a:
                        counter = counter + 1
                if(float(counter) / len(data_router_b) >= float(perc) / 100):
                    data.pop(router_b, None)
#                    print "Tak"
#                print "Nie"
#                elif counter > 0:
#                    print counter, len(data_router_b), len(data_router_a)
                    
                #                if(set([item[0] for item in data[router_b]]).issubset(set([item[0] for item in data[router_a]]))):
#                    data.pop(router_b, None)
    print len(data.keys())
    return data
    
def merge_routers_weighted(data, perc):
    print len(data.keys())
    
#    for router in data.items():
#        print router
    res = OrderedDict()
    
    router_set_desc = OrderedDict(sorted(data.items(), reverse = True, key=lambda x: len(x[1]))).keys()
    router_set_asc = OrderedDict(sorted(data.items(), reverse = False, key=lambda x: len(x[1]))).keys()
    for router_a in router_set_asc:
        for router_b in router_set_desc:
            if(data.has_key(router_b) and data.has_key(router_a) and router_b != router_a):
                data_router_a = set([item[0] for item in data[router_a]])
                data_router_b = set([item[0] for item in data[router_b]])
                
                counter = 0
                for item in data_router_b:
                    if item in data_router_a:
                        counter = counter + 1
                if(float(counter) / len(data_router_b) >= float(perc) / 100):
                    num = (len(data[router_a]) + len(data[router_b])) / (len(data[router_a]))
                    data.pop(router_b, None)
                    if res.has_key(router_a):
                        res[router_a] = res[router_a] + num
                    else:
                        res[router_a] = num
#                    print "Tak"
#                print "Nie"
#                elif counter > 0:
#                    print counter, len(data_router_b), len(data_router_a)
                    
                #                if(set([item[0] for item in data[router_b]]).issubset(set([item[0] for item in data[router_a]]))):
#                    data.pop(router_b, None)
    print len(data.keys())
    return [data, res]
    
def merge_routers_weighted_in_parts(data, perc, min_date, max_date, merging_delta):
    print len(data.keys())

    time_delta = datetime.timedelta(hours = merging_delta)
    start_date = datetime.datetime(min_date.year, min_date.month, min_date.day, min_date.hour, 0)
    end_date = start_date + time_delta
    curr_date = start_date
    
    merged_routers_all = OrderedDict()
    merge_counted_all = OrderedDict()
    while curr_date < max_date:
        small_data = OrderedDict()
        for router in data.keys():
            dates = []
            for s_date in data[router]:
                if s_date[0] < end_date and s_date[0] >= curr_date:
                    dates.append(s_date)
            if len(dates) > 0:
                small_data[router] = dates
        curr_date = end_date
        end_date = curr_date + time_delta
        # calc weighted jaccard
        [merged_routers, merge_counted] =  merge_routers_weighted(small_data, perc)
        # merge results
        for router in merged_routers:
            if merged_routers_all.has_key(router):
                merged_routers_all[router] = merged_routers_all[router] + merged_routers[router]
            else:
                merged_routers_all[router] = merged_routers[router]
        for router in merge_counted:
            if merge_counted_all.has_key(router):
                merge_counted_all[router] = merge_counted_all[router] + merge_counted[router]
            else:
                merge_counted_all[router] = merge_counted[router]

    return [merged_routers_all, merge_counted_all]
    
def merge_routers_two(data, class_data, start_date, end_date):
    print len(data.keys())
    router_set_desc = OrderedDict(sorted(data.items(), reverse = True, key=lambda x: len(x[1]))).keys()
    counter = 0
    for router_a in router_set_desc:
        if(data.has_key(router_a)):

            to_remove = []
            for router_b in router_set_desc:
                if(data.has_key(router_b) and router_b != router_a):
                    if(set([item[0] for item in data[router_b]]).issubset(set([item[0] for item in data[router_a]]))):
                        to_remove.append(router_b)
                        
            # chage data to routers data
            if len(to_remove) > 0:
#                output.plot_merging(data, class_data, router_a, to_remove, start_date, end_date, "merge_iteration_" + str(counter) + ".pdf", (8,12), True)
                for item in to_remove:
                    data.pop(item, None)
                counter = counter + 1
                print counter
        
#                data_router_a = set([item[0] for item in data[router_a]])
#                data_router_b = set([item[0] for item in data[router_b]])
#                counter = 0
#                for item in data_router_b:
#                    if item in data_router_a:
#                        counter = counter + 1
#                if(float(counter) / len(data_router_b) >= float(perc) / 100):
#                    data.pop(router_b, None)
                #elif counter > 0:
                #    print counter, len(data_router_b), len(data_router_a)
                    
                #                if(set([item[0] for item in data[router_b]]).issubset(set([item[0] for item in data[router_a]]))):
#                    data.pop(router_b, None)
    print len(data.keys())
    return data